{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-4">
	{if condition="$id eq 0"}
	<h3 class="pb-3">添加员工信息</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">员工姓名<font>*</font></td>
			<td>
				<input type="text" name="name" lay-verify="required" placeholder="请输入员工姓名" lay-reqText="请输入员工姓名" class="layui-input">
			</td>
			<td class="layui-td-gray">员工性别<font>*</font></td>
			<td>
				<input type="radio" name="sex" value="1" title="男">
				<input type="radio" name="sex" value="2" title="女">
			</td>
			<td class="layui-td-gray">初始密码</td>
			<td>
				{$reg_pwd}
				<input type="hidden" name="reg_pwd" value="{$reg_pwd}" lay-verify="required">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">手机号码<font>*</font></td>
			<td>
				<input type="text" name="mobile" lay-verify="required|phone" lay-reqText="请输入正确的手机号码" placeholder="可使用手机号码登录" class="layui-input">
			</td>
			<td class="layui-td-gray">电子邮箱<font>*</font></td>
			<td>
				<input type="text" name="email" lay-verify="required|email" lay-reqText="请输入正确的电子邮箱" placeholder="请输入电子邮箱" class="layui-input">
			</td>
			<td rowspan="3" class="layui-td-gray">头像<br /><span style="font-size: 12px;">(如若不上传<br />系统将自动生成)</span></td>
			<td rowspan="3" valign="top" style="width: 120px;">
				<div class="layui-upload" style="width: 120px;">
					<div class="layui-upload-list" id="uploadBox" style="width: 100%; height:120px; overflow: hidden;">
						<img src="/static/home/images/nonepic600x600.jpg" height="120" style="max-width: 100%; width: 100%;" />
						<input type="hidden" name="thumb" value="">
					</div>
					<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" style="width: 100%;" id="uploadBtn">上传头像</button>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">主部门<font>*</font></td>
			<td><select id="did" name="did" xm-selected="" xm-select="select2" xm-select-skin="default" xm-select-radio="true"></select></td>
			<td class="layui-td-gray">次要部门</td>
			<td><select id="department_ids" name="department_ids" xm-selected="" xm-select="select1" xm-select-skin="default"></select></td>
		</tr>
		<tr>
			<td class="layui-td-gray">岗位职称<font>*</font></td>
			<td>
				<select name="position_id" lay-verify="required" lay-reqText="请选择岗位职称">
					<option value="">请选择岗位职称</option>
					{volist name="position" id="v"}
						<option value="{$v.id}">{$v.title}</option>
					{/volist}
				</select>				
			</td>
			<td class="layui-td-gray">上级主管</td>
			<td>
				<input type="text" name="pname" readonly placeholder="请选择" class="layui-input picker-admin">
				<input type="hidden" name="pid" value="0">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">入职日期<font>*</font></td>
			<td>
				<input type="text" name="entry_time" readonly lay-verify="required" placeholder="请选择入职日期" lay-reqText="请选择入职日期" class="layui-input tool-time">
			</td>
			<td class="layui-td-gray">身份类型<font>*</font></td>
			<td>
				<input type="radio" name="is_staff" value="1" title="企业员工">
				<input type="radio" name="is_staff" value="2" title="劳动派遣">
				<input type="radio" name="is_staff" value="3" title="兼职人员">
			</td>
			<td class="layui-td-gray-2">隐藏联系方式</td>
			<td>
				<input type="radio" name="is_hide" value="0" title="否" checked>
				<input type="radio" name="is_hide" value="1" title="是">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">员工类型<font>*</font></td>
			<td colspan="5">
				<input type="radio" name="type" value="1" title="正式">
				<input type="radio" name="type" value="2" title="试用">
				<input type="radio" name="type" value="3" title="实习">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">数据权限<font>*</font></td>
			<td colspan="5">
				<input type="radio" name="auth_did" value="0" title="仅自己关联的数据" checked>
				<input type="radio" name="auth_did" value="1" title="所属主部门的数据">				
				<input type="radio" name="auth_did" value="2" title="所属次部门的数据">				
				<input type="radio" name="auth_did" value="3" title="所属主次部门的数据">				
				<input type="radio" name="auth_did" value="4" title="所属主部门及其子部门数据">				
				<input type="radio" name="auth_did" value="5" title="所属次部门及其子部门数据">				
				<input type="radio" name="auth_did" value="6" title="所属主次部门及其子部门数据">				
				<input type="radio" name="auth_did" value="7" title="所属主部门所在顶级部门及其子部门数据">				
				<input type="radio" name="auth_did" value="8" title="所属次部门所在顶级部门及其子部门数据">				
				<input type="radio" name="auth_did" value="9" title="所属主次部门所在顶级部门及其子部门数据">				
				<input type="radio" name="auth_did" value="10" title="所有部门数据">			
			</td>
		</tr>
	</table>
	{else/}
	<h3 class="pb-3">编辑员工信息</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">员工姓名<font>*</font></td>
			<td>
				<input type="text" name="name" value="{$detail.name}" lay-verify="required" placeholder="请输入员工姓名" lay-reqText="请输入员工姓名" class="layui-input">
			</td>
			<td class="layui-td-gray">登录账号</td>
			<td>
				{$detail.username}
			</td>
			<td class="layui-td-gray">初始密码</td>
			<td>
				{empty name="$detail.reg_pwd"}
					密码已重置
				{else/}
					{$detail.reg_pwd}
				{/empty}
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">手机号码<font>*</font></td>
			<td>
				<input type="text" name="mobile" value="{$detail.mobile}" lay-verify="required|phone" lay-reqText="请输入正确的手机号码" placeholder="可使用手机号码登录" class="layui-input">
			</td>
			<td class="layui-td-gray">电子邮箱<font>*</font></td>
			<td>
				<input type="text" name="email" value="{$detail.email}" lay-verify="required|email" lay-reqText="请输入正确的电子邮箱" placeholder="请输入电子邮箱" class="layui-input">
			</td>
			<td rowspan="4" class="layui-td-gray">头像<br /><span style="font-size: 12px;">(如若不上传<br />系统将自动生成)</span></td>
			<td rowspan="4" valign="top" style="width: 160px;">
				<div class="layui-upload" style="width: 160px;">
					<div class="layui-upload-list" id="uploadBox" style="width: 100%; height:150px; overflow: hidden;">
						<img src="{$detail.thumb}" height="150" style="max-width: 100%;" />
						<input type="hidden" name="thumb" value="{$detail.thumb}">
					</div>
					<button type="button" class="layui-btn layui-btn-normal" style="width: 100%;" id="uploadBtn">上传头像</button>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">员工性别<font>*</font></td>
			<td>
				<input type="radio" name="sex" value="1" title="男" {eq name="$detail.sex" value="1"}checked{/eq}>
				<input type="radio" name="sex" value="2" title="女" {eq name="$detail.sex" value="2"}checked{/eq}>
			</td>
			<td class="layui-td-gray">入职日期<font>*</font></td>
			<td>
				<input type="text" value="{$detail.entry_time|date='Y-m-d'}" name="entry_time" readonly lay-verify="required" placeholder="请选择入职日期" lay-reqText="请选择入职日期" class="layui-input tool-time">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">主部门<font>*</font></td>
			<td><select id="did" name="did" xm-selected="{$detail.did}" xm-select="select2" xm-select-skin="default" xm-select-radio="true"></select></td>
			<td class="layui-td-gray">次要部门</td>
			<td><select id="department_ids" name="department_ids" xm-selected="{$detail.department_ids}" xm-select="select1" xm-select-skin="default"></select></td>
		</tr>
		<tr>
			<td class="layui-td-gray">岗位职称<font>*</font></td>
			<td>
				<select name="position_id" lay-verify="required" lay-reqText="请选择岗位职称">
					<option value="">请选择岗位职称</option>
					{volist name="position" id="v"}
						<option value="{$v.id}" {eq name="$v.id" value="$detail.position_id"}selected{/eq}>{$v.title}</option>
					{/volist}
				</select>				
			</td>
			<td class="layui-td-gray">上级主管</td>
			<td>
				<input type="text" name="pname" value="{$detail.pname|default=''}" readonly placeholder="请选择" class="layui-input picker-admin">
				<input type="hidden" name="pid" value="{$detail.pid}">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">员工类型<font>*</font></td>
			<td>
				<input type="radio" name="type" value="1" title="正式" {eq name="$detail.type" value="1"}checked{/eq}>
				<input type="radio" name="type" value="2" title="试用" {eq name="$detail.type" value="2"}checked{/eq}>
				<input type="radio" name="type" value="3" title="实习" {eq name="$detail.type" value="3"}checked{/eq}>
			</td>
			<td class="layui-td-gray">身份类型<font>*</font></td>
			<td>
				<input type="radio" name="is_staff" value="1" title="企业员工" {eq name="$detail.is_staff" value="1"}checked{/eq}>
				<input type="radio" name="is_staff" value="2" title="劳动派遣" {eq name="$detail.is_staff" value="2"}checked{/eq}>
				<input type="radio" name="is_staff" value="3" title="兼职员工" {eq name="$detail.is_staff" value="3"}checked{/eq}>
			</td>
			<td class="layui-td-gray-3">隐藏联系方式</td>
			<td>
				<input type="radio" name="is_hide" value="0" title="否" {eq name="$detail.is_hide" value="0"}checked{/eq}>
				<input type="radio" name="is_hide" value="1" title="是" {eq name="$detail.is_hide" value="1"}checked{/eq}>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">数据权限<font>*</font></td>
			<td colspan="5">
				<input type="radio" name="auth_did" value="0" title="仅自己关联的数据" {eq name="$detail.auth_did" value="0"}checked{/eq}>
				<input type="radio" name="auth_did" value="1" title="所属主部门的数据" {eq name="$detail.auth_did" value="1"}checked{/eq}>				
				<input type="radio" name="auth_did" value="2" title="所属次部门的数据" {eq name="$detail.auth_did" value="2"}checked{/eq}>				
				<input type="radio" name="auth_did" value="3" title="所属主次部门的数据" {eq name="$detail.auth_did" value="3"}checked{/eq}>				
				<input type="radio" name="auth_did" value="4" title="所属主部门及其子部门数据" {eq name="$detail.auth_did" value="4"}checked{/eq}>				
				<input type="radio" name="auth_did" value="5" title="所属次部门及其子部门数据" {eq name="$detail.auth_did" value="5"}checked{/eq}>				
				<input type="radio" name="auth_did" value="6" title="所属主次部门及其子部门数据" {eq name="$detail.auth_did" value="6"}checked{/eq}>				
				<input type="radio" name="auth_did" value="7" title="所属主部门所在顶级部门及其子部门数据" {eq name="$detail.auth_did" value="7"}checked{/eq}>				
				<input type="radio" name="auth_did" value="8" title="所属次部门所在顶级部门及其子部门数据" {eq name="$detail.auth_did" value="8"}checked{/eq}>				
				<input type="radio" name="auth_did" value="9" title="所属主次部门所在顶级部门及其子部门数据" {eq name="$detail.auth_did" value="9"}checked{/eq}>					
				<input type="radio" name="auth_did" value="10" title="所有部门数据" {eq name="$detail.auth_did" value="10"}checked{/eq}>				
			</td>
		</tr>
	</table>
	{/if}

	<div class="pt-4">
		<input type="hidden" value="{$id}" name="id">
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
	</div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool','formSelects','oaPicker','uploadPlus'];
	function gouguInit() {
		var form = layui.form,tool=layui.tool,formSelects=layui.formSelects,oaPicker = layui.oaPicker,uploadPlus=layui.uploadPlus;
		
		//选择所属部门
		var selected_a = $('#did').attr('xm-selected');
		formSelects.data('select2', 'server', {
			url: '/api/index/get_department_select',
			keyword: selected_a
		});
		//选择所属其他部门
		var selected_b = $('#department_ids').attr('xm-selected');
		formSelects.data('select1', 'server', {
			url: '/api/index/get_department_select',
			keyword: selected_b
		});
		
		//头像上传
		var photoUpload = new uploadPlus({
			'use':'single',
			'callback':function(res){
				layer.msg(res.msg);
				if (res.code == 0) {
					//上传成功
					$('#uploadBox input').attr('value', res.data.filepath);
					$('#uploadBox img').attr('src', res.data.filepath);
				}
			}
		});

		//监听提交
		form.on('submit(webform)', function(data){
			if(!data.field.sex){
				layer.msg('请选择员工性别');
				return false;
			}
			if(!data.field.type){
				layer.msg('请选择员工类型');
				return false;
			}
			if(!data.field.is_staff){
				layer.msg('请选择员工身份');
				return false;
			}
			let department_ids = data.field.department_ids.split(',');
			if(department_ids.includes(data.field.did)){
				layer.msg('次要部门不能包含主部门');
				return false;
			}
			let callback = function (e) {
				layer.msg(e.msg);
				if (e.code == 0) {
					tool.sideClose(1000);
				}
			}
			let clickbtn = $(this);
			tool.post("/user/user/add", data.field, callback,clickbtn);
			return false;
		});
	}

</script>
{/block}
<!-- /脚本 -->